package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class fragment_MainActivity extends AppCompatActivity implements View.OnClickListener{

    Fragment fragment1,fragment2,fragment3,fragment4;

    LinearLayout layout1,layout2,layout3,layout4;
    
    FragmentManager manager;
    FragmentTransaction transaction;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_main);
        
        layout1=findViewById(R.id.bottom_linearlayout1);
        layout2=findViewById(R.id.bottom_linearlayout2);
        layout3=findViewById(R.id.bottom_linearlayout3);
        layout4=findViewById(R.id.bottom_linearlayout4);
        
        fragment1=new Fragment1();
        fragment2=new Fragment2();
        fragment3=new Fragment3();
        fragment4=new Fragment4();

        manager=getSupportFragmentManager();
        transaction= manager.beginTransaction();
        
        initial();

        fragmentHide();

        transaction.show(fragment1);
        transaction.commit();

        layout1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                transaction=manager.beginTransaction();
                fragmentHide();
                transaction.show(fragment1);
                transaction.commit();
            }
        });

        layout2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                transaction=manager.beginTransaction();
                fragmentHide();
                transaction.show(fragment2);
                transaction.commit();
            }
        });

        layout3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                transaction=manager.beginTransaction();
                fragmentHide();
                transaction.show(fragment3);
                transaction.commit();
            }
        });

        layout4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                transaction=manager.beginTransaction();
                fragmentHide();
                transaction.show(fragment4);
                transaction.commit();
            }
        });





    }

    private void initial() {
        transaction.add(R.id.framelayout,fragment1);
        transaction.add(R.id.framelayout,fragment2);
        transaction.add(R.id.framelayout,fragment3);
        transaction.add(R.id.framelayout,fragment4);


    }

    void fragmentHide(){

        transaction.hide(fragment1);
        transaction.hide(fragment2);
        transaction.hide(fragment3);
        transaction.hide(fragment4);

        //transaction.show(fragment1);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.bottom_linearlayout1) {
            showfragment(fragment1);

        } else if (id == R.id.bottom_linearlayout2) {
            showfragment(fragment2);

        } else if (id == R.id.bottom_linearlayout3) {
            showfragment(fragment3);

        } else if (id == R.id.bottom_linearlayout4) {
            showfragment(fragment4);
        }
    }

    private void showfragment(Fragment fragment) {
        transaction=manager.beginTransaction();
        fragmentHide();
        transaction.show(fragment);
        transaction.commit();
    }
}